import { Body, Controller, Post } from '@nestjs/common';
import { PrismaService } from 'src/prisma/prisma.service';
import { BorrowService } from './borrow.service';

@Controller('borrow')
export class BorrowController {
  constructor(private prisma: PrismaService, private borrow: BorrowService) {}

  @Post('/')
  async addBorrow(@Body() Param) {
    try {
      return {
        data: await this.prisma.borrows.create({
          data: {
            book: { connect: { bookId: Param.bookBookId } },
            StuID: { connect: { StuId: Param.userId } },
          },
        }),
        success: true,
      };
    } catch (err) {
      return {
        message: '该图书被借阅',
        success: false,
      };
    }
  }

  @Post('/return')
  async returnBorrow(@Body() Param) {
    return await this.borrow.returnBorrow(Param.bookId);
  }

  @Post('/forceReturn')
  async forceReturnBorrow(@Body() Param) {
    return {
      success: true,
      data: await this.borrow.forceReturnBorrow(Param.bookId),
    };
  }
}
